package com.ikun.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.ikun.entity.Attendances;
import com.ikun.entity.Users;
import com.ikun.service.AttendancesService;
import com.ikun.service.NoticesService;
import com.ikun.service.ProperManagementService;
import com.ikun.service.UserService;
import com.ikun.util.Result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Controller
@RequestMapping("/manager")
@Tag(name = "管家页面", description = "房产管理")
public class PropertyManagementController {
    @Autowired
    /**
     * 管家页面展示业主的所有信息
     */
    public ProperManagementService properManagementService;

    @Autowired
    public AttendancesService attendancesService;

    @Autowired
    public NoticesService noticesService;

    @Autowired
    private UserService userService;

    @GetMapping("/properties")
    @ResponseBody
    @Operation(summary = "管家id下房产的所有信息")
    public Result showAllProperties(String managementId) {
        return Result.success(properManagementService.findAllByManagementId(managementId));
    }

    // @GetMapping("/propertiesById")
    // @ResponseBody
    // @Operation(summary = "根据管家id查询房产的所有信息")
    // public Result showAllPropertiesById(Integer managementId) {
    // return Result.success(properManagementService.findAllByManagementId());
    // }

    /**
     * 管家页面查询房产状态，模糊查询楼栋号
     */
    @GetMapping("/search")
    @ResponseBody
    @Operation(summary = "根据楼栋id查询房产状态")
    public Result findByBuildingidAndStatus(Integer buildingId) {
        return Result.success(properManagementService.findByBuildingidAndStatus(buildingId));
    }

    @GetMapping("/owners")
    @ResponseBody
    @Operation(summary = "业主所有信息")
    public Result showAllOwners() {
        return Result.success(properManagementService.findAllOwner());
    }

    @PostMapping("/addOwner")
    @ResponseBody
    @Operation(summary = "添加业主信息")
    public Result addOwner(String propertyId, String name, String phone, String password) {
        Users users = new Users();
        users.setName(name);
        users.setPhone(phone);
        users.setPassword(password);
        System.out.println("users = " + users);
        String msg = properManagementService.addOwner(users, propertyId);
        return Result.success(msg);
    }

    @GetMapping("/attendanceCurrentTime")
    @ResponseBody
    @Operation(summary = "获取当前时间")
    public Result getCurrentTime() {
        return Result.success(LocalDateTime.now());
    }

    @PostMapping("/attendanceclock_in")
    @ResponseBody
    @Operation(summary = "上班打卡", description = "根据当前时间判断打卡状态，超过 12:00 无法打卡")
    public Result clock_in(Integer request) {
        LocalTime currentTime = LocalTime.now();
        return attendancesService.clock_in(request, currentTime);
    }

    @PostMapping("/attendanceclockout")
    @Operation(summary = "下班打卡", description = "根据当前时间判断下班打卡状态")
    public Result clock_out(Integer request) {
        LocalTime currentTime = LocalTime.now();
        return attendancesService.clock_out(request, currentTime);
    }

    @PostMapping("/attendanceleave")
    @Operation(summary = "提交请假申请", description = "员工提交请假申请")
    public Result insertStatus(Integer request, LocalDate recordDate) {
        return attendancesService.insertStatus(request, recordDate, 4);
    }

    @GetMapping("/attendancerecords")
    @Operation(summary = "查询指定时间的考勤记录", description = "根据日期范围查询考勤记录")
    public Result<List<Attendances>> getAttendanceRecords(
        @Parameter(description = "开始日期，格式：YYYY-MM-DD") @RequestParam("start_date") LocalDate startDate,
        @Parameter(description = "结束日期，格式：YYYY-MM-DD") @RequestParam("end_date") LocalDate endDate) {
        // 构建查询参数
        Map<String, Object> params = Map.of("start_date", startDate, "end_date", endDate);
        // 调用Service层方法进行查询
        return attendancesService.getAttendanceRecords(params);
    }

    @PostMapping("/announcements")
    @ResponseBody
    @Operation(summary = "发布公告")
    public Result insertAnnouncements(String content, String title) {
        return Result.success(noticesService.addNotice(content, title));
    }

    @GetMapping("/owner/id")
    @ResponseBody
    @Operation(summary = "根据业主id查询业主基本信息,电话,名字")
    public Result showAllOwnerInfoById(Integer id) {
        return Result.success(userService.findAllOwnerInfoById(id));
    }

    @PostMapping("/updateOwner")
    @ResponseBody
    @Operation(summary = "根据业主id更改业主信息")
    public Result updateOwner(String ownerId, String phone, String photoUrl) {
        if (userService.updateOwner(ownerId, phone, photoUrl) > 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 加载员工列表
     */
    @GetMapping("/staff")
    @ResponseBody
    @Operation(summary = "加载员工列表")
    public Result showAllStaff() {
        return Result.success(userService.findAllStaff());
    }
}
